/**
 * symbols
 * 自ECMAScript 2015起，symbol成为了一种新的原生类型
 * symbol类型的值是通过Symbol构造函数创建的
 */

let sym1 = Symbol('key') // 可选的字符串key
let sym2 = Symbol('key')
console.log(sym1 === sym2) //false
console.log(sym1) //Symbol(key)

// 用作对象的key
/*let obj = {
  [sym1]: 'zhangsan',
}
console.log(obj[sym1]) //zhangsan*/

// Symbol.iterator属性的值是一个函数，它返回一个迭代器对象。
interface Person {
  name: string
  age: number
  hobbies: Array<string>
  [Symbol.iterator]():object
}
class person implements Person{
  constructor(name:string,age:number,hobbies:Array<string>) {
    this.name = name
    this.age = age
    this.hobbies = hobbies
  }
  name=''
  age=17
  hobbies=[''];
  *[Symbol.iterator](){
    // console.log(this);
  let keys = Object.keys(this)
    for (let i = 0; i < keys.length; i++) {
      yield {key: keys[i], value: this[keys[i] as keyof this]}
      // yield 产生
    }
}
}

let p = new person('zhangsan',18,['sing','dance','rap'])
console.log('@',p);
/*@ person {
  name: 'zhangsan',
  age: 18,
  hobbies: [ 'sing', 'dance', 'rap' ]
}*/

for (let {key,value} of p) {
  console.log('key: ',key,'; value: ',value);
}
/*key:  name ; value:  zhangsan
key:  age ; value:  18
key:  hobbies ; value:  [ 'sing', 'dance', 'rap' ]*/





/*let person: Person = {
  name: 'zhangsan',
  age: 18,
  hobbies: ['sing', 'dance', 'rap'],
  [Symbol.iterator]() {
    const _this = this
    const keys = Reflect.ownKeys(_this) //获取对象的key值列表
    let index = 0
    return {
      next: function () {
        if (index < keys.length - 1) {
          return {
            value: _this[keys[index++]],
            done: false,
          }
        }
        return {
          value: keys[index++],
          done: true,
        }
      },
    }
  },
}

for (let p of person) {
  console.log(p)
}*/
